package com.uinnova.product.eam.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.binary.core.exception.BinaryException;
import com.binary.jdbc.Page;
import com.uinnova.product.eam.dto.AXEAClassMapping;
import com.uinnova.product.eam.service.AXEAConfService;
import com.uinnova.product.eam.service.SystemService;
import com.uinnova.product.eam.vo.SystemQueryVo;
import com.uinnova.product.eam.dto.SimpSystemInfo;
import com.uinnova.product.vmdb.comm.model.ci.CcCi;
import com.uinnova.product.vmdb.provider.ci.bean.CcCiInfo;
import com.uinnova.product.vmdb.provider.ci.bean.CiGroupPage;
import com.uino.bean.cmdb.base.ESCIClassInfo;
import com.uino.bean.cmdb.base.LibType;
import com.uino.bean.cmdb.query.ESCISearchBean;
import com.uino.dao.cmdb.ESCIClassSvc;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class SystemServiceImpl implements SystemService {

    @Autowired
    private AXEAConfService axeaConfService;
    @Autowired
    private IamsCISwitchSvc ciSwitchSvc;
    @Autowired
    private ESCIClassSvc classSvc;

    @Override
    public Page<SimpSystemInfo> list(SystemQueryVo queryVo) {
        Assert.notNull(queryVo, "分页参数不能为空");
        Assert.notNull(queryVo.getPageNum(), "第几页不能为空");
        Assert.notNull(queryVo.getPageSize(), "每页显示记录数不能为空");
        AXEAClassMapping axeaClassMapping = axeaConfService.getSystemClassMapping();

        //无label字段时返回空
        List<String> labelDefs = getLabelDefs(axeaClassMapping.getSystemClassId());
        if (CollectionUtils.isEmpty(labelDefs)) {
            return new Page<>(queryVo.getPageNum(), queryVo.getPageSize(), 0, 0, new ArrayList<>());
        }
        ESCISearchBean bean = new ESCISearchBean();
        bean.setSortField("modifyTime");
        bean.setAsc(false);
        if (!StringUtils.isBlank(queryVo.getKeyword())) {
            bean.setWordLabel(true);
            bean.setWords(Arrays.asList(queryVo.getKeyword()));
        }
        bean.setClassIds(Arrays.asList(axeaClassMapping.getSystemClassId()));
        bean.setDomainId(1L);
        bean.setPageNum(queryVo.getPageNum());
        bean.setPageSize(queryVo.getPageSize());
        CiGroupPage page = ciSwitchSvc.queryPageBySearchBean(bean, false, LibType.DESIGN);
        if (CollectionUtils.isEmpty(page.getData())) {
            return new Page<>(queryVo.getPageNum(), queryVo.getPageSize(), 0, 0, new ArrayList<>());
        }
        List<SimpSystemInfo> simpSystemInfos = new ArrayList<>();
        for (CcCiInfo ciInfo : page.getData()) {
            String label = getLabel(ciInfo.getCi());
            SimpSystemInfo simpSystemInfo = new SimpSystemInfo();
            simpSystemInfo.setLabel(label);
            simpSystemInfo.setCiCode(ciInfo.getCi().getCiCode());
            simpSystemInfos.add(simpSystemInfo);
        }
        return new Page<>(queryVo.getPageNum(), queryVo.getPageSize(), page.getTotalRows(), page.getTotalPages(), simpSystemInfos);
    }

    private String getLabel(CcCi ci) {
        String ciLabel = ci.getCiLabel();
        if (StringUtils.isBlank(ciLabel)) {
            return null;
        }
        List<String> datas = JSONObject.parseArray(ciLabel, String.class);
        if (CollectionUtils.isEmpty(datas)) {
            return null;
        }
        return String.join("-", datas);
    }

    private List<String> getLabelDefs(Long classId) {
        BoolQueryBuilder query = QueryBuilders.boolQuery();
        query.must(QueryBuilders.termQuery("id", classId));
        List<ESCIClassInfo> ciClassInfos = classSvc.getSourceCIClassDefByQuery(query);
        if (CollectionUtils.isEmpty(ciClassInfos)) {
            return new ArrayList<>();
        }
        List<String> defs = new ArrayList<>();
        for (ESCIClassInfo cls : ciClassInfos) {
            cls.getCcAttrDefs().forEach(def -> {
                if (def.getIsCiDisp() != null && def.getIsCiDisp() == 1) {
                    defs.add(def.getProStdName());
                }
            });
        }
        return defs;
    }

    @Override
    public List<SimpSystemInfo> queryByCiCodes(List<String> ciCodes) {
        if (CollectionUtils.isEmpty(ciCodes)) {
            return new ArrayList<>();
        }
        AXEAClassMapping axeaClassMapping = axeaConfService.getSystemClassMapping();
        //无label字段时返回空
        List<String> labelDefs = getLabelDefs(axeaClassMapping.getSystemClassId());
        if (CollectionUtils.isEmpty(labelDefs)) {
            throw new BinaryException("系统分类暂无label字段");
        }
        ESCISearchBean bean = new ESCISearchBean();
        bean.setCiCodes(ciCodes);
        bean.setClassIds(Arrays.asList(axeaClassMapping.getSystemClassId()));
        bean.setDomainId(1L);
        bean.setPageNum(1);
        bean.setPageSize(ciCodes.size());
        CiGroupPage page = ciSwitchSvc.queryPageBySearchBean(bean, false, LibType.DESIGN);
        if (CollectionUtils.isEmpty(page.getData())) {
            return new ArrayList<>();
        }
        List<SimpSystemInfo> simpSystemInfos = new ArrayList<>();
        for (CcCiInfo ciInfo : page.getData()) {
            String label = getLabel(ciInfo.getCi());
            SimpSystemInfo simpSystemInfo = new SimpSystemInfo();
            simpSystemInfo.setLabel(label);
            simpSystemInfo.setCiCode(ciInfo.getCi().getCiCode());
            simpSystemInfos.add(simpSystemInfo);
        }
        return simpSystemInfos;
    }
}
